#include "stdafx.h"
#include "Tutorial.h"

void Tutorial::Show(const char* bg, std::vector<const char*>& images,
	std::vector<sf::Vector2f>& positions, sf::RenderWindow& window)
{
	bool finished = false;
	int iterator = -1;
	float lastXSize = 0.f;

	bool skipFlag = false;

	sf::Texture bgTexture;
	bgTexture.loadFromFile(bg);
	sf::Sprite bgSprite;
	bgSprite.setTexture(bgTexture, true);
	bgSprite.setColor(sf::Color(255,255,255,0));
	bgSprite.setScale(2.f, 1.f);

	sf::View view;

	sf::Sprite* sprites = new sf::Sprite[images.size()];
	sf::Texture* textures = new sf::Texture[images.size()];

	for (int i = 0; i < images.size(); ++i)
	{
		textures[i].loadFromFile(images[i]);
		textures[i].setSmooth(true);
		sprites[i].setTexture(textures[i], true);
		sprites[i].setPosition(positions[i]);
		sprites[i].setScale(.0f,.0f);
		sprites[i].setOrigin(sprites[i].getLocalBounds().width * 0.5f, sprites[i].getLocalBounds().height * 0.5f); 
	}

	float accumulator = 0.0f;
	float timer = 0.0f;
	bool goLeft = false;
	sf::Clock frameClock;

	while(window.isOpen() && !finished)
	{
		sf::Event event;

		while(window.pollEvent(event))
		{
			if(event.type == sf::Event::Closed)
				window.close();
			else if ( event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape )
			{
				window.close();
			}   
		}

		sf::Vector2u screenSize = window.getSize();
		if(screenSize.x == 0) screenSize.x = 0;
		view.setSize(sf::Vector2f(600.0f *  screenSize.x / screenSize.y, 600.0f));
		view.setCenter(view.getSize() * 0.5f);

		if (lastXSize != view.getSize().x)
		{
			float xShift = (view.getSize().x - 800.f) * 0.5f;
			for (int i = 0; i < images.size(); ++i)
				sprites[i].setPosition(positions[i].x + xShift, positions[i].y);
		}

		accumulator += frameClock.getElapsedTime().asSeconds();
		frameClock.restart();

		if(accumulator > 6.0f / 60.0f) accumulator = 6.0f / 60.0f;

		while(accumulator >= 1.0f / 60.0f)
		{
			accumulator -= 1.0f / 60.0f;
			
			bool skipAnim = false;
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
			{
				if (!skipFlag)
				{
					skipFlag = true;
					skipAnim = true;
				}
			}
			else
				skipFlag = false;

			if (iterator == -1)
			{
				if (timer >= 1.f || skipAnim)
				{
					bgSprite.setColor(sf::Color::White);
					++iterator;
					timer = 0.f;
				}
				else
				{
					bgSprite.setColor(sf::Color(255,255,255, 255.0f * timer));
					timer += DELTA;
				}
			}
			else if (iterator < images.size())
			{
				if (timer >= 2.5f || skipAnim)
				{
					sprites[iterator].setScale(1.f,1.f);
					++iterator;
					timer = 0.f;
				}
				else if (timer <= 1.5f)
				{
					float t = timer / 1.5f;
					float scale = 1.f*(float)pow(2,-10*t) * (float)sin( (t-0.075)*(2*PI)/0.3 ) + 1;
					sprites[iterator].setScale(scale, scale);
					timer += DELTA;
				}
				else
				{
					sprites[iterator].setScale(1.f,1.f);
					timer += DELTA;
				}
			}
			else if (iterator == images.size())
			{
				if (skipAnim)
					++iterator;
			}
			else
			{
				if (timer >= 1.f || skipAnim)
				{
					bgSprite.setColor(sf::Color(255,255,255,0));
					finished = true;
				}
				else
				{
					sf::Color alpha(255,255,255, 255.f * (1.f - timer));
					bgSprite.setColor(alpha);
					for (int i = 0; i < images.size(); ++i)
						sprites[i].setColor(alpha);
					timer += DELTA;
				}
			}
		}

		window.setView(view);
		window.clear(sf::Color(0, 0, 0));


		window.draw(bgSprite);

		for (int i = 0; i < images.size(); ++i)
		{
			if (sprites[i].getScale().x > .0f)
				window.draw(sprites[i]);
		}

		window.display();
	}

	if (sprites)
		delete[] sprites;
	if (textures)
		delete[] textures;
}